using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
using System.Configuration;
namespace DAL
{
  public class tbKhachHangDAL
  {
      private SqlConnection cn = null;
      public tbKhachHangDAL()
      {
          cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
          cn.Open();
      }

      public tbKhachHangDAL(tbKhachHangDAL tan)
      {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
      }

      public void Insert(tbKhachHang th)
      {
            SqlCommand cm = new SqlCommand("tbKhachHang_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@email", th.email);
            cm.Parameters.AddWithValue("@matkhau", th.matkhau);
            cm.Parameters.AddWithValue("@hotenkh", th.hotenkh);
            cm.Parameters.AddWithValue("@ngaysinh", th.ngaysinh);
            cm.Parameters.AddWithValue("@gioitinh", th.gioitinh);
            cm.Parameters.AddWithValue("@diachi", th.diachi);
            cm.Parameters.AddWithValue("@dienthoai", th.dienthoai);
            cm.Parameters.AddWithValue("@sotaikhoan", th.sotaikhoan); 
            cm.ExecuteNonQuery();
        }
        public void Update(tbKhachHang th)
        {
            SqlCommand cm = new SqlCommand("tbKhachHang_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@makh", th.makh);
            cm.Parameters.AddWithValue("@email", th.email);
            cm.Parameters.AddWithValue("@matkhau", th.matkhau);
            cm.Parameters.AddWithValue("@hotenkh", th.hotenkh);
            cm.Parameters.AddWithValue("@ngaysinh", th.ngaysinh);
            cm.Parameters.AddWithValue("@gioitinh", th.gioitinh);
            cm.Parameters.AddWithValue("@diachi", th.diachi);
            cm.Parameters.AddWithValue("@dienthoai", th.dienthoai);
            cm.Parameters.AddWithValue("@sotaikhoan", th.sotaikhoan); 
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tbKhachHang_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@makh", iD);
            cm.ExecuteNonQuery();
        }
        public tbKhachHangCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbKhachHang_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbKhachHangCollection khachhangCollection = new tbKhachHangCollection();
            while (dr.Read())
            {
                tbKhachHang th = new tbKhachHang();
                th.makh = int.Parse(dr.GetValue(0).ToString());
                th.email = dr.GetValue(1).ToString();
                th.matkhau = dr.GetValue(2).ToString();
                th.hotenkh = dr.GetValue(3).ToString();
                th.ngaysinh = dr.GetValue(4).ToString();
                th.gioitinh  = int.Parse(dr.GetValue(5).ToString());
                th.diachi = dr.GetValue(6).ToString();
                th.dienthoai = dr.GetValue(7).ToString();
                th.sotaikhoan  = float.Parse(dr.GetValue(8).ToString());
                khachhangCollection.Add(th);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return khachhangCollection;
        }
    }
}
